package com.gdyx.app.web.filter;

import com.gdyx.app.web.model.MpsOrgUser;
import com.gdyx.app.web.model.MpsUser;
import com.gdyx.app.web.svc.MpsOrgUserSvc;
import com.gdyx.app.web.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;


public class MpsInterceptor extends BaseController implements HandlerInterceptor {

    @Autowired
    MpsOrgUserSvc mpsOrgUserSvc;

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        //获取请求的URL
        String webPath = req.getContextPath();
        String uri = req.getRequestURI();

        if (uri.equals(webPath + "/logout") || uri.equals(webPath + "/doLogin" ) || uri.equals(webPath + "/doRegister")  ||  uri.equals(webPath + "/MpsUser/setRegUser") ||uri.equals(webPath + "/findOrgNum" ) || uri.equals(webPath + "/MpsOrg/changeOrg" ) ) {
          return true;
        }
        MpsUser user = (MpsUser)req.getSession().getAttribute("ADMIN_SESSION");
        //获取Session
        String org_session = (String)req.getSession().getAttribute("Org_SESSION");
        if(user==null){
            return true;
        }else {
            if (org_session!=null){
                return true;
            }else {
                List<MpsOrgUser> mpsOrgUsersByUid = mpsOrgUserSvc.findMpsOrgUsersByUid(user.getId());
                Iterator<MpsOrgUser> iterator = mpsOrgUsersByUid.iterator();
                while (iterator.hasNext()) {
                    MpsOrgUser mpsOrgUser = iterator.next();
                    if(mpsOrgUser.getOrgchk()!=0 || mpsOrgUser.getUserchk()!=0){
                        iterator.remove();
                    }
                }
                if(mpsOrgUsersByUid.size()==0){
                    //没有机构
                    req.getRequestDispatcher("/MpsOrg/ApplyOrg/goMpsApplyOrgList").forward(req, resp);
                }
                if(mpsOrgUsersByUid.size()==1){
                    String orgId = mpsOrgUsersByUid.get(0).getOrgId();
                    req.getSession().setAttribute("Org_SESSION",orgId);
                    //获取权限
                    GetFunByUser(user.getId(),orgId);
                    req.getRequestDispatcher("/goIndex").forward(req, resp);
                }
                if(mpsOrgUsersByUid.size()>1){
                    req.getRequestDispatcher("/findOrgNum").forward(req, resp);
                }
            }
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
